#include "stdafx.h"
#include "ServerConsole.h"
#include "ZServerSocketNetworkController.h"
#include "ZRandomNumberGenerator.h"
#include "TestGameMessageHandler.h"
#include "GameMessage.h"

using	namespace	Z;

template<>	ServerConsole*	Singleton<ServerConsole>::msSingleton	= nullptr;

ServerConsole::ServerConsole()
:	mMessageManager(new ServerSocketNetworkController(12345, 0), new TestGameMessageHandler),
	mTransfering(false)
{
}

ServerConsole::~ServerConsole()
{
}

bool ServerConsole::handle(char command)
{
	if(!Win32Console::handle(command))
		return false;
	
	switch(command)	{
		case 't':
		case 'T':
			mTransfering	= !mTransfering;
			break;
	}
	return true;
}

bool ServerConsole::initialise()
{
	if(!Win32Console::initialise())
		return false;

	if(!mMessageManager.start())
		return false;

	return true;
}

void ServerConsole::finalise()
{
	Win32Console::finalise();

	mMessageManager.cleanup();
}

void ServerConsole::doProcess(float frameTime)
{
	static float accumTime	= 0.0f;
//	const size_t kMaxDataSize	= 8192;
//	const size_t kMinDataSize	= 1;
//	static byte_t data[kMaxDataSize];
//	const size_t dataSize	= random((int)kMinDataSize, (int)kMaxDataSize);
	accumTime	+= frameTime;
	mMessageManager.execute();
	if(0.2f < accumTime)	{
		accumTime	-= 0.2f;
		if(mTransfering)	{
			mMessageManager.send(ChatGameMessage("this is string"), ServerSocketNetworkController::all);
		}
	}
}